#include "mpi_lib.h"
#include "sample_venc_osd.h"
#include "mdi_common.h"
#include "lcd_common.h"
#include "osd.h"

static unsigned char gOSD_Configure_flag[4] = {0,0,0,0};
int g_osd_fonttype = OSD_FONT_FREETYPE_TYPE; //0 freetype   1 lattice
static OSD_TextParamsT gOSD_DateParams[OSD_PLANE_NUM][OSD_AREA_NUM];

void osd_configure(unsigned int streamId)
{
    int i = 0;
    if(streamId >= VENC_STREAM_NUM)
        return ;

    if(gOSD_Configure_flag[streamId] != 0)
        return ;

    gOSD_Configure_flag[streamId] = 1;
    switch(streamId)
    {
        case 0:
                memset(&(gOSD_DateParams[streamId][0]),0,sizeof(OSD_TextParamsT));
                gOSD_DateParams[streamId][0].enable = 1;
                gOSD_DateParams[streamId][0].bold = 0;
                gOSD_DateParams[streamId][0].color = OSD_COLOUR_YELLOW;
                strcat(gOSD_DateParams[streamId][0].fontsPath,"/usr/share/fonts/Vera.ttf");
                gOSD_DateParams[streamId][0].startX = 0;
                gOSD_DateParams[streamId][0].startY = 0;
                gOSD_DateParams[streamId][0].boxHeight = 40;
                gOSD_DateParams[streamId][0].boxWidth = 40;
                gOSD_DateParams[streamId][0].size = 24;
                gOSD_DateParams[streamId][0].background = OSD_COLOUR_YELLOW;
                gOSD_DateParams[streamId][0].outline = 6;
                
                memset(&(gOSD_DateParams[streamId][1]),0,sizeof(OSD_TextParamsT));
                gOSD_DateParams[streamId][1].enable = 1;
                gOSD_DateParams[streamId][1].bold = 0;
                gOSD_DateParams[streamId][1].color = OSD_COLOUR_GREEN;
                strcat(gOSD_DateParams[streamId][1].fontsPath,"/usr/share/fonts/Vera.ttf");
                gOSD_DateParams[streamId][1].startX = 0;
                gOSD_DateParams[streamId][1].startY = 0;
                gOSD_DateParams[streamId][1].boxHeight = 40;
                gOSD_DateParams[streamId][1].boxWidth = 40;
                gOSD_DateParams[streamId][1].size = 24;
                gOSD_DateParams[streamId][1].background = OSD_COLOUR_YELLOW;
                gOSD_DateParams[streamId][1].outline = 6;
                
                memset(&(gOSD_DateParams[streamId][2]),0,sizeof(OSD_TextParamsT));
                gOSD_DateParams[streamId][2].enable = 1;
                gOSD_DateParams[streamId][2].bold = 1;
                gOSD_DateParams[streamId][2].color = OSD_COLOUR_RED;
                strcat(gOSD_DateParams[streamId][2].fontsPath,"/usr/share/fonts/Vera.ttf");
                gOSD_DateParams[streamId][2].startX = 0;
                gOSD_DateParams[streamId][2].startY = 0;
                gOSD_DateParams[streamId][2].boxHeight = 40;
                gOSD_DateParams[streamId][2].boxWidth = 40;
                gOSD_DateParams[streamId][2].size = 24;
                gOSD_DateParams[streamId][2].background = OSD_COLOUR_YELLOW;
                gOSD_DateParams[streamId][2].outline = 6;
            break;
        case 1:
        case 2:
        case 3:
            for(i=0; i<OSD_AREA_NUM; i++)
            {

                memset(&(gOSD_DateParams[streamId][i]),0,sizeof(OSD_TextParamsT));
                gOSD_DateParams[streamId][i].enable = 1;
                gOSD_DateParams[streamId][i].bold = 0;
                gOSD_DateParams[streamId][i].color = OSD_COLOUR_YELLOW;
                strcat(gOSD_DateParams[streamId][i].fontsPath,"/usr/share/fonts/Vera.ttf");
                gOSD_DateParams[streamId][i].startX = 0;
                gOSD_DateParams[streamId][i].startY = 0;
                gOSD_DateParams[streamId][i].boxHeight = 80;
                gOSD_DateParams[streamId][i].boxWidth = 80;
                gOSD_DateParams[streamId][i].size = 24;
                gOSD_DateParams[streamId][i].background = OSD_COLOUR_YELLOW;
                gOSD_DateParams[streamId][i].outline = 6;
            }
            break;
        default:
            break;
    }
}

int osd_process(sgks_mpi_venc_stream_format_param_s *pVencStreamFormatParams)
{
    int nRet = 0;
    char pData[30] = {0};
    u16 streamId, areaId, encodeWidth, encodeHeight;

    streamId = pVencStreamFormatParams->streamId;
    encodeWidth= pVencStreamFormatParams->width;
    encodeHeight = pVencStreamFormatParams->height;

    if(streamId >= VENC_STREAM_NUM)
        return SGKS_ERR_OSD_PARAMERR;

    memset(gOSD_DateParams[streamId][0].textStr, 0, OSD_TEXT_MAX_LENGTH);
    memset(gOSD_DateParams[streamId][1].textStr, 0, OSD_TEXT_MAX_LENGTH);
    memset(gOSD_DateParams[streamId][2].textStr, 0, OSD_TEXT_MAX_LENGTH);
    sys_get_date(pData);
    gOSD_DateParams[streamId][0].enable = 1;
    strcat(gOSD_DateParams[streamId][0].textStr,pData);
    gOSD_DateParams[streamId][0].textLen = strlen(gOSD_DateParams[streamId][0].textStr);

    gOSD_DateParams[streamId][1].enable = 1;
    strcat(gOSD_DateParams[streamId][1].textStr,pData);
    gOSD_DateParams[streamId][1].textLen = strlen(gOSD_DateParams[streamId][1].textStr);

    gOSD_DateParams[streamId][2].enable = 1;
    strcat(gOSD_DateParams[streamId][2].textStr,pData);
    gOSD_DateParams[streamId][2].textLen = strlen(gOSD_DateParams[streamId][2].textStr);    
    if (g_osd_fonttype == 1)
    {
        if(SGKS_SUCCESS != osd_lattice_initFont())
        {
            DEBUGMSG(ZONE_ERROR, ("%s: load font file error!\n", __FUNCTION__));
            return OSD_PROCESS_FAIL;
        }
        areaId = 0;
        nRet = osd_lattice_start(encodeWidth, encodeHeight, streamId, areaId, &gOSD_DateParams[streamId][areaId]);
        if (nRet != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("osd_function_show_date error!\n"));
            return OSD_PROCESS_FAIL;
        }

        areaId = 1;
        nRet = osd_lattice_start(encodeWidth, encodeHeight, streamId, areaId, &gOSD_DateParams[streamId][areaId]);
        if (nRet != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("osd_function_show_date error!\n"));
            return OSD_PROCESS_FAIL;
        }

        areaId = 2;
        nRet = osd_lattice_start(encodeWidth, encodeHeight, streamId, areaId, &gOSD_DateParams[streamId][areaId]);
        if (nRet != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("osd_function_show_date error!\n"));
            return OSD_PROCESS_FAIL;
        }        
    }
    else
    {
        areaId = 0;
        nRet = osd_function_show_date(encodeWidth, encodeHeight, streamId, areaId, &gOSD_DateParams[streamId][areaId]);
        if (nRet != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("osd_function_show_date error!\n"));
            return OSD_PROCESS_FAIL;
        }
        areaId = 1;
        nRet = osd_function_show_date(encodeWidth, encodeHeight, streamId, areaId, &gOSD_DateParams[streamId][areaId]);
        if (nRet != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("osd_function_show_date error!\n"));
            return OSD_PROCESS_FAIL;
        }
        areaId = 2;
        nRet = osd_function_show_date(encodeWidth, encodeHeight, streamId, areaId, &gOSD_DateParams[streamId][areaId]);
        if (nRet != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("osd_function_show_date error!\n"));
            return OSD_PROCESS_FAIL;
        }
    }
    return nRet;
}

int osd_show(sgks_mpi_venc_stream_format_param_s *pVencStreamFormatParams)
{
    int nRet = SGKS_SUCCESS;
    u16 streamId = pVencStreamFormatParams->streamId;

    if(streamId >= VENC_STREAM_NUM)
        return SGKS_ERR_OSD_PARAMERR;

    osd_configure(streamId);

    nRet = osd_process(pVencStreamFormatParams);
    if(nRet != SUCCESS)
    {
        DEBUGMSG(ZONE_OSD, ("osd_process failed!\n"));
        return OSD_PROCESS_FAIL;
    }
    return nRet;
}


int sgks_mpi_sample_venc_osd_start(int osd_font_type)
{
    int ret = SGKS_SUCCESS;

    ret = osd_init();
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: osd init fail\n", __FUNCTION__));
        return ret;
    }
    g_osd_fonttype = osd_font_type;
    ret = osd_open(g_osd_fonttype);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: osd open fail\n", __FUNCTION__));
        return ret;
    }

    DEBUGMSG(ZONE_OSD, ("%s: osd_font_type=%d\n", __FUNCTION__, osd_font_type));

    sgks_mpi_venc_stream_format_param_s VencStreamFormatParams;
    memset(&VencStreamFormatParams, 0x00, sizeof(VencStreamFormatParams));
    VencStreamFormatParams.streamId = 0;
    ret = sgks_mpi_venc_get_params(VENC_PARAM_TYPE_STREAM, (void *)&VencStreamFormatParams);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s:get stream 0 param fail\n", __FUNCTION__));
        return ret;
    }

    if(VencStreamFormatParams.encodeType != 0)
    {
        ret = osd_show(&VencStreamFormatParams);
        if(ret != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("%s:show osd fail\n", __FUNCTION__));
            return ret;
        }
    }

    //if (g_osd_fonttype == OSD_FONT_FREETYPE_TYPE)
    {
        memset(&VencStreamFormatParams, 0x00, sizeof(VencStreamFormatParams));
        VencStreamFormatParams.streamId = 1;
        ret = sgks_mpi_venc_get_params(VENC_PARAM_TYPE_STREAM, (void *)&VencStreamFormatParams);
        if(ret != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("%s:get stream 0 param fail\n", __FUNCTION__));
            return ret;
        }

        if(VencStreamFormatParams.encodeType != 0)
        {
            ret = osd_show(&VencStreamFormatParams);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s:show osd fail\n", __FUNCTION__));
                return ret;
            }
        }
    }
    return ret;
}
